package com.avzipper;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;

public class Compressor {
	
	public static final int MSG_SETDATA_DONE = 1;
	public static final int MSG_CONVERT_DONE = 2;
	public static final int MSG_OUTPUT_NOTSET = 3;
	
	public static final int UNKNOWN_STREAM = -1;
	public static final int NO_STREAM = -2;
	
	static {
		NativeTester nativeTester = new NativeTester();
		if(nativeTester.isNeon()){
			System.loadLibrary("ffmpeg-neon");
			System.loadLibrary("ffmpeg-jni-neon");
		}else{
			System.loadLibrary("ffmpeg");
			System.loadLibrary("ffmpeg-jni");
		}
	}
	
	//this is the address in memory of native compressor
	private int nativeCompressor;
	private FFmpegStreamInfo[] _streamsInfo = null;
	private boolean isEncoding = false;
	private Context _context;
	private Handler _handler;
	
	public Compressor(Context context, Handler handler){
		_context = context;
		_handler = handler;
	}
	
	private void setStreamsInfo(FFmpegStreamInfo[] streamsInfos){
		_streamsInfo = streamsInfos;
	}
	
	public FFmpegStreamInfo[] getStreamsInfo(){
		return _streamsInfo;
	}
		
	//I hardcode here to test
	public void startEncode(String inputFile, String outputFile){
		
		if(inputFile == null || inputFile.equals("")){
			_handler.sendEmptyMessage(MSG_OUTPUT_NOTSET);
			return;
		}
		
		if(outputFile == null || outputFile.equals("")){
			_handler.sendEmptyMessage(MSG_OUTPUT_NOTSET);
			return;
		}
		
		//(new EncodeTask()).execute("ffmpeg", "-i", inputFile, "-filter:v","scale=-1:360", "-acodec", "copy", outputFile);
		(new EncodeTask()).execute("ffmpeg", "-i", inputFile, "-s","480x360","-vcodec", "copy", "-acodec", "copy", outputFile);
	}
		
	private void handleSignalFromNative(int msgID){
		android.util.Log.i("Compressor-Java", "Get signal from native " + msgID);
		switch(msgID){
			case MSG_CONVERT_DONE:
				android.util.Log.i("Compressor-Java", "Convert done !!!!!!!");
				_handler.sendEmptyMessage(MSG_CONVERT_DONE);
				break;
		}
	}
	
	// native methods
	native int encodeVideo(String[] args);	
	
	/***********************************AsyncTasks************************************************/	
	private class EncodeTask extends AsyncTask<String, Void, Integer>{

		@Override
		protected Integer doInBackground(String... params) {
			//int ret = encodeVideo(params[0], params[1], params[2]);
			int ret = encodeVideo(params);
			return ret;
		}
		
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
		};
		
	}
}
